﻿package kliment.display.draw.shape {
	import flash.geom.Point;
	import kliment.display.draw.DrawStyle;
	
	/**
	 * ...
	 * @author Kliment
	 */
	public class ArcShape extends ShapeAbstract {
		static public const CHORD:String = "chord";
		static public const OPEN:String = "open";
		static public const PIE:String = "pie";
		
		public function ArcShape(center:Point, radius:Number = 100, start:Number = 0, end:Number = 360, type:String = CHORD, yRadius:Number = 0, drawStyle:DrawStyle = null) {
			super();
			center.x = (type != PIE)?center.x:center.x-radius;
			if (yRadius == 0)
				yRadius = radius;
			var angleMid:Number, ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number;
			
			if (Math.abs(end) > 360) {
				end = 360;
			}
			var segs:Number = Math.ceil(Math.abs(end) / 45);
			var segAngle:Number = end / segs;
			var theta:Number = -(segAngle / 180) * Math.PI;
			var angle:Number = -(start / 180) * Math.PI;
			if(type == CHORD || type == OPEN) {
				ax = center.x - Math.cos(angle) * radius;
				ay = center.y - Math.sin(angle) * yRadius;
			}
			if (segs > 0) {	
				_setStyles(graphics, drawStyle);
				graphics.moveTo(center.x, center.y);
				if(type == PIE) {
					ax = center.x + Math.cos(start / 180 * Math.PI) * radius;
					ay = center.y + Math.sin( -start / 180 * Math.PI) * yRadius;
					graphics.lineTo(ax, ay);
					ax = center.x;
					ay = center.y;
				}
				for(var i:Number = 0;  i < segs; i++) {
					angle += theta;
					angleMid = angle - (theta / 2);
					bx = ax + Math.cos(angle) * radius;
					by = ay + Math.sin(angle) * yRadius;
					cx = ax + Math.cos(angleMid) * (radius/Math.cos(theta/2));
					cy = ay + Math.sin(angleMid) * (yRadius/Math.cos(theta/2));
					graphics.curveTo(cx, cy, bx, by);
				}
				if(type == OPEN) {
					if (drawStyle.lineStyle != null) graphics.lineStyle(0,0,0);
					graphics.lineTo(center.x, center.y);
					graphics.lineStyle(drawStyle.lineStyle.width, drawStyle.lineStyle.color, drawStyle.lineStyle.alpha, drawStyle.lineStyle.pixelHinting, drawStyle.lineStyle.scaleMode, drawStyle.lineStyle.caps, drawStyle.lineStyle.joints, drawStyle.lineStyle.miterLimit);
					//setLineStyle(arc, drawStyle.lineStyle);
				} else {
					graphics.lineTo(center.x, center.y);
				}
				graphics.endFill();
			}
		}
		
	}
	
}